import parole
from parole.colornames import colors
from parole.display import interpolateRGB
import pygame, random

import sim, main
from util import *
from dungeon import makeFloor, Room, TemplateRoom
import dungeon, town

# template keys:
#
#   1  2  3
#    #####
#    #   #
#  8 # 9 # 4
#    #   #
#    #####
#   7  6  5
templates = {
        1: [
"""
.....
.....
.....
.....
....#
""",
"""
.....
.....
.9...
.....
....#
""",
"""
.....
.....
.*...
...**
...*#
""",
"""
.....
.....
..---
*.|..
..|.#
""",
"""
.*###
.##_o
.o__#
.##_#
.*#+#
""",
"""
.....
.vv|.
.vv|.
.vv|.
....#
""",
"""
.....
.**..
.*x..
..**.
....#
""",
            ],

        2: [
"""
.....
.....
.....
.....
#####
""",
"""
.----
.....
----.
.....
#####
""",
"""
|.|..
|.|.|
|.|.|
|.|.|
#####
""",
"""
.----
.....
----.
*****
#####
""",
"""
.....
.....
.....
.....
##o##
""",
"""
.....
.vvv.
.vvv.
.....
##o##
""",
"""
.----
.....
----.
.....
##o##
""",
"""
.....
.....
.....
**.**
##o##
""",
"""
----.
.....
.....
**.**
##o##
""",
"""
.....
.....
.....
.....
##+##
""",
"""
.....
.....
.|.|.
.|.|.
##+##
""",
"""
.*.*.
.*.*.
.*.*.
.*.*.
##+##
""",
"""
..*.*
***..
**..*
...**
##+##
""",
"""
.....
.....
.....
.....
##+##
""",
"""
.....
.....
.#+#.
##_##
#___#
""",
"""
.*.*.
**.**
*#+#*
##_##
#___#
""",
"""
.*.*.
.*.*.
.#+#.
o#_#o
#___#
""",
"""
x*...
**...
.o+o.
##_##
#___#
""",
"""
.***.
##o##
o___o
#___#
##+##
""",
"""
.***.
##o##
+___+
#___#
#___#
""",
            ],

        3: [
"""
.....
.....
.....
.....
#....
""",
"""
..###
..o_o
..#_#
..+_#
#.###
""",
"""
.....
.....
.*|*|
**|*|
#*|*|
""",
"""
.*...
.....
.....
**...
#*...
""",
"""
.....
.9...
..vvv
..vvv
#.vvv
""",
            ],

        4: [
"""
#....
#....
#....
#....
#....
""",
"""
#*...
#*...
#*...
#*...
#*...
""",
"""
#*...
#**..
#***.
#**.*
#*.*.
""",
"""
#...|
#..*|
o..*|
#...|
#..*|
""",
"""
#....
#***.
+....
#***.
o....
""",
"""
##...
_##*.
__o*.
_##*.
##*..
""",
"""
##...
_##*.
__+..
_##*.
##...
""",
"""
##...
_#o#.
___+.
_#o#.
##...
""",
"""
#....
##o##
____#
___##
#o##.
""",
"""
#vvvv
##o##
____+
___##
#o##.
""",
"""
#o##.
___##
___+.
___##
####.
""",
"""
#....
#.###
#.#_#
#.#0#
#....
""",
"""
#....
#.###
#.#d#
#.#0#
#....
""",
"""
#.#0#
#.#_#
#.###
#.vvv
o.vvv
""",
"""
#.#0#
#.#d#
#.###
#.vvv
o.vvv
""",
"""
#....
#..*.
o.*x*
#.*..
#....
""",
"""
#....
#..*.
#..x*
#....
#**..
""",
"""
#----
#.**.
#**x.
#....
#----
""",
"""
#....
#.vvv
#.vvv
#.vvv
#....
""",
            ],
        
        5: [
"""
#....
.....
.....
.....
.....
""",
"""
#....
...*.
##+##
#___#
#####
""",
"""
#....
.....
.*...
*x*..
.*...
""",
"""
#....
.###.
*#_0.
*###.
.***.
""",
"""
#.vvv
.9vvv
vvvvv
vvvvv
vvvvv
""",
"""
#.**.
.....
v|v|v
v|v|v
v|v|v
""",
            ],
        
        6: [
"""
#####
.....
.....
..*..
.....
""",
"""
#####
.....
.....
.....
.....
""",
"""
#####
.....
.vvv.
9vvv.
.....
""",
"""
##o##
*****
.###.
.#_0.
.###.
""",
"""
##o##
*****
.###.
.#d0.
.###.
""",
"""
#o#o#
..|..
..|..
..|..
.....
""",
"""
#+#o#
.....
.....
..vvv
..vvv
""",
"""
#+###
.....
..*x.
.**..
.....
""",
"""
#___#
#___#
#_###
#_#|*
#o#|*
""",
"""
#___#
#___#
#+#_#
..#_#
**#o#
""",
"""
#___#
##_##
*#+#*
**.**
**.**
""",
"""
##+##
#___#
#___#
#o+o#
.....
""",
            ],
        
        7: [
"""
....#
.....
.....
.....
.....
""",
"""
*...#
**...
.*..*
..***
..*..
""",
"""
**..#
.....
##+#.
o__o.
####.
""",
"""
vvv.#
vvv9.
vvvvv
vvvvv
vvvvv
""",
"""
|.|.#
|.|--
|....
|----
.***.
""",
"""
....#
.....
.x.|.
...|.
...|.
""",
            ],
        
        8: [
"""
....#
....#
....#
....#
....#
""",
"""
....#
.*..#
.**.#
.**.#
..*.#
""",
"""
....#
.*..#
.**.o
.**.#
..*.#
""",
"""
***.#
*x**#
....+
****#
....#
""",
"""
....#
#0#.#
#_#.#
###.#
....#
""",
"""
....#
#0#.#
#_#.#
###.#
....#
""",
"""
....o
*####
.+__+
*####
.---o
""",
"""
##o##
#____
+____
#____
##o##
""",
"""
##o##
#____
+_@__
#____
##o##
""",
            ],
        
        9: [
                #"""
                #_____
                #_____
                #_____
                #_____
                #_____
                #""",
                #"""
                #__#__
                #__#__
                #__+__
                #__#__
                #__#__
                #""",
                #"""
                #_____
                #_____
                ###+##
                #_____
                #_____
                #""",
"""
___#_
___+_
___#_
#+###
___#_
""",
"""
___#_
_@_+_
___#_
#+###
___#_
""",
"""
___#_
#+###
___#_
___+_
___#_
""",
"""
_#___
###+#
_#___
_+___
_#___
""",
"""
_#___
###+#
_#___
_+_@_
_#___
""",
"""
_#___
_+___
_#___
###+#
_#___
""",
"""
_#___
_+_@_
_#___
###+#
_#___
""",
            ],
        
}

wallTypes = [
    # (name, bgcolor)
    ('clay wall', colors['Brown']),
    ('clay wall', colors['Tan']),
    ('rough brick wall', colors['DarkRed']),
    ('wood slatted wall', colors['Chocolate']),
    ('wood slatted wall', colors['Sienna']),
    ('wood slatted wall', colors['SaddleBrown']),
    ('wood slatted wall', colors['Chocolate']),
    ('wood slatted wall', colors['Sienna']),
    ('wood slatted wall', colors['SaddleBrown']),
    ('wood slatted wall', colors['Chocolate']),
    ('wood slatted wall', colors['Sienna']),
    ('wood slatted wall', colors['SaddleBrown']),
    ('wood slatted wall', colors['Chocolate']),
    ('wood slatted wall', colors['Sienna']),
    ('wood slatted wall', colors['SaddleBrown']),
    ('slatestone wall', colors['SlateGray']),
    ('painted wall', colors['RoyalBlue']),
    ('painted wall', colors['OliveDrab']),
]

floorTypes = [
    # (name, char, fgcolor, bgcolor)
    ('sawdust-covered floor', '.', colors['Gray'], colors['DarkKhaki']),
]


segHeight = 5
segWidth = 5

floorColor1 = colors['Olive']
floorColor2 = colors['DarkGreen']
floorName = 'grass'
mixture = 0.3

diggable = None # means perimeter

def fill(s, width, fillchar=' '):
    if len(s) >= width:
        return s
    return s + fillchar * (len(s) - width)

def floorGenerator(floorName, floorChar, floorFg, floorBg):
    return parole.map.MapObjectGenerator("", lambda: town.HouseFloor(floorName,
        10, parole.map.AsciiTile(floorChar, floorFg, bg_rgb=floorBg)),
        clearFirst=True)

class House(TemplateRoom):
    def __init__(self, pos):

        numHorizontal, numVertical = random.randint(3,4), random.randint(3,4)
        numMids = numHorizontal - 2

        template = []
        for bigRowNum in xrange(numVertical):
            if bigRowNum == 0:
                left, right = random.choice(templates[1]),\
                              random.choice(templates[3]) 
                mids = [random.choice(templates[2]) for m in xrange(numMids)]
            elif bigRowNum == numVertical-1:
                left, right = random.choice(templates[7]),\
                              random.choice(templates[5]) 
                mids = [random.choice(templates[6]) for m in xrange(numMids)]
            else:
                left, right = random.choice(templates[8]),\
                              random.choice(templates[4]) 
                mids = [random.choice(templates[9]) for m in xrange(numMids)]

            left = [fill(x.strip(), segWidth) for x in left.strip().split('\n')]
            right = [fill(x.strip(), segWidth) for x in right.strip().split('\n')]
            mids = [[fill(x.strip(), segWidth) for x in mid.strip().split('\n')]\
                    for mid in mids]

            for r in xrange(segHeight):
                row = left[r]
                for m in xrange(numMids):
                    row += mids[m][r]
                row += right[r]
                template.append(row)

        templateStr = '\n'.join(template)
        parole.debug('House template:\n%s' % templateStr)

        wallName, wallColor = random.choice(wallTypes)
        floorName, floorChar, floorFg, floorBg = random.choice(floorTypes)
        baseShrubR, baseShrubG, baseShrubB = colors['DarkGreen']
        legend = {
             '#': parole.map.MapObjectGenerator("", 
                    lambda: sim.Obj(wallName, 10, parole.map.AsciiTile(' ',
                        colors['Black'], bg_rgb=wallColor),
                        blockMove=True, blockLOS=True, massNoun=False, startsVowel=False),
                    clearFirst=True),

             '_': floorGenerator(floorName, floorChar, floorFg, floorBg),

             '+': (floorGenerator(floorName, floorChar, floorFg, floorBg), 
                 parole.map.MapObjectGenerator("doorGenerator", 
                 lambda: sim.Door('wooden door', 100, parole.map.AsciiTile('/',
                     colors['White'], bg_rgb=wallColor), parole.map.AsciiTile('+',
                         colors['White'], bg_rgb=wallColor)))),
             
             '0': parole.map.MapObjectGenerator("doorGenerator", 
                 lambda: sim.Door('outhouse door', 100, parole.map.AsciiTile('/',
                     colors['White'], bg_rgb=wallColor), parole.map.AsciiTile('+',
                         colors['White'], bg_rgb=wallColor))),
             
             'o': parole.map.MapObjectGenerator("windowGenerator", 
                    lambda: sim.Obj('glass window', 100, 
                        parole.map.AsciiTile(' ', colors['PaleTurquoise'],
                            bg_rgb=colors['PaleTurquoise']), blockLOS=False,
                        blockMove=True), clearFirst=True),

             '*': parole.map.MapObjectGenerator("roseGenerator",
                    lambda: sim.Obj('shrub', -2, parole.map.AsciiTile('*',
                        (baseShrubR + random.randint(-64,64), baseShrubG,
                            baseShrubB)))),

             '|': parole.map.MapObjectGenerator("windowGenerator", 
                    lambda: sim.Obj('grapevine trellis', 100, 
                        parole.map.AsciiTile('|', colors['Peru'],
                            bg_rgb=None), blockLOS=False,
                        blockMove=True), clearFirst=False),

             '-': parole.map.MapObjectGenerator("windowGenerator", 
                    lambda: sim.Obj('grapevine trellis', 100, 
                        parole.map.AsciiTile('-', colors['Peru'],
                            bg_rgb=None), blockLOS=False,
                        blockMove=True), clearFirst=False),

             'v': parole.map.MapObjectGenerator("roseGenerator",
                    lambda: sim.Floor('vegetable garden', 10, parole.map.AsciiTile('v',
                        (baseShrubR + random.randint(-64,64), baseShrubG,
                            baseShrubB), bg_rgb=colors['DarkGoldenRod'])),
                        clearFirst=True),

             'x': parole.map.MapObjectGenerator("windowGenerator", 
                    lambda: sim.Obj('stone well', 100, 
                        parole.map.AsciiTile('o', colors['White'],
                            bg_rgb=colors['Gainsboro']), blockLOS=False,
                        blockMove=True), clearFirst=True),

             '@': (floorGenerator(floorName, floorChar, floorFg, floorBg),
                   dungeon.thingGenerator('npcs', 'housewife')),
            
             '9':  dungeon.thingGenerator('npcs', 'housewife'),
            
             'd': (floorGenerator(floorName, floorChar, floorFg, floorBg),
                   dungeon.thingGenerator('npcs', 'drunkard')),
            
             '.':   None,
        
        }


        TemplateRoom.__init__(self, templateStr, legend, None, pos,
                None, None, None, None)


roomClass = House




